package GUI.forms;

import i18n.JButtonI18N;
import i18n.JLabelI18N;
import i18n.LanguageUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import GUI.EnginesManager;
import GUI.GUI;
import GUI.common.ExceptionI18N;
import GUI.common.Item;
import GUI.common.model.BasicComboModel;
import board.Game;
import engine.ChessEngine;

public class FAnalyzer extends GeneralForm {
	private EnginesManager em=null;
	private List<Game> games;

	public FAnalyzer(GUI gui, List<Game> games) {
		super(gui);
		this.games= games;
		try {
			em= new EnginesManager(new File("./settings/engines"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		initComponents();
		loadGamesTable();
		//loadEngines();
		
		// TODO Auto-generated constructor stub
	}
	
	
	
	private void loadGamesTable(){    	    	
    	Object[][] model=null; 
		int i =0;		
		if(games!=null){
			model = new Object[games.size()][5];
			for(Game game:games){
				model[i][0]=game.getInfo().get("White");
				model[i][1]=game.getInfo().get("Black");
				model[i][2]=game.getInfo().get("Event");
				model[i][3]=game.getInfo().get("Round");					
				model[i][4]=game.getInfo().get("Date");
				i++;
			}
		}
		
		TableModel dataModel = new DefaultTableModel(model, new String [] {			        		    
				LanguageUtils.getMessage("Form.Analyze.Game.White"), 
				LanguageUtils.getMessage("Form.Analyze.Game.Black"),
				LanguageUtils.getMessage("Form.Analyze.Game.Event"),
				LanguageUtils.getMessage("Form.Analyze.Game.Round"),
				LanguageUtils.getMessage("Form.Analyze.Game.Date")
		});		
		this.jTEvents.setModel(dataModel);		
    }
	
	public void setFileDestination(String file){
		this.jLDestinationFile.setText(file);
		String test = this.jLDestinationFile.getText();
	}
	
	private void loadComboEngines(){
		ArrayList<Item> items = new ArrayList<Item>();
		int i=0;
		for(ChessEngine engine: em.getEngines()){
			items.add(new Item(i, engine.getName(), engine));
			i++;
		}
		
		jCEngine.setModel(new BasicComboModel(items));
		jCEngine.setSelectedIndex(0);    	
    }
	
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTEvents = new javax.swing.JTable();
        jBAnalyze = new JButtonI18N("Form.Analyze.Start", this, "cmd.StartAnalyze") ;
        jLSelectGame = new JLabelI18N("Form.Analyze.SelectGame");
        jBSelectFile = new JButtonI18N("Form.Analyze.SelectFileDest", this, "cmd.FileDestination") ;
        jLDestinationFile = new JLabel("");
        jLTime = new JLabelI18N("Form.Analyze.Time");
        jTTime = new javax.swing.JTextField();
        jLErrorLimit =  new JLabelI18N("Form.Analyze.ErrorLimit");
        jTErrorLimit = new javax.swing.JTextField();
        jLBorder = new JLabelI18N("Form.Analyze.Border");
        jTBorder = new javax.swing.JTextField();
        jLMaxMistakes = new JLabelI18N("Form.Analyze.MaxMistakes");
        jTMaxMistakes = new javax.swing.JTextField();
        jLEngine = new JLabelI18N("Form.Analyze.SelectEngine");
        jCEngine= new JComboBox();
        
        
        jTTime.setText("15");        
        jTErrorLimit.setText("25");
        jTBorder.setText("400");
        jTMaxMistakes.setText("8");
        
        loadComboEngines();

        
        jTEvents.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTEvents.setName("jTGames"); // NOI18N
        jScrollPane1.setViewportView(jTEvents);

        
        

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jBAnalyze, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                    .addComponent(jLSelectGame, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 466, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBSelectFile, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLDestinationFile, javax.swing.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLTime, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jTTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLErrorLimit, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jTErrorLimit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLBorder, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jTBorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLMaxMistakes, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jTMaxMistakes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))    
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jLEngine, javax.swing.GroupLayout.DEFAULT_SIZE, 360, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jCEngine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLSelectGame)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 182, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBSelectFile)
                    .addComponent(jLDestinationFile))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLTime)
                    .addComponent(jTTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLErrorLimit)
                    .addComponent(jTErrorLimit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLBorder)
                    .addComponent(jTBorder, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLMaxMistakes)
                    .addComponent(jTMaxMistakes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLEngine)
                    .addComponent(jCEngine, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 40, Short.MAX_VALUE)
                .addComponent(jBAnalyze)
                .addContainerGap())
        );
    }// </editor-fold>


    // Variables declaration - do not modify
    private JButtonI18N jBAnalyze;
    private JButtonI18N jBSelectFile;
    private JLabelI18N jLSelectGame;
    private javax.swing.JLabel jLDestinationFile;
    private JLabelI18N jLTime;
    private JLabelI18N jLErrorLimit;
    private JLabelI18N jLBorder;
    private JLabelI18N jLMaxMistakes;
    private JLabelI18N jLEngine;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTEvents;
    private javax.swing.JTextField jTTime;
    private javax.swing.JTextField jTErrorLimit;
    private javax.swing.JTextField jTBorder;
    private javax.swing.JTextField jTMaxMistakes;
    private javax.swing.JComboBox jCEngine;

	@Override
	protected boolean validation() throws ExceptionI18N {
		// TODO Auto-generated method stub
		return false;
	}



	public JLabel getjLDestinationFile() {
		return jLDestinationFile;
	}



	public javax.swing.JTable getjTEvents() {
		return jTEvents;
	}



	public javax.swing.JTextField getjTTime() {
		return jTTime;
	}



	public javax.swing.JTextField getjTErrorLimit() {
		return jTErrorLimit;
	}



	public javax.swing.JTextField getjTBorder() {
		return jTBorder;
	}



	public javax.swing.JTextField getjTMaxMistakes() {
		return jTMaxMistakes;
	}
	
	 public Game getSelectedGame(){
	    	int selectedRow = this.jTEvents.getSelectedRow();
	    	return (selectedRow!=-1?this.games.get(selectedRow):null);
	  }
	
	 public ChessEngine getSelectedEngine(){
	    	int index = this.jCEngine.getSelectedIndex();
			return (index!=-1? (ChessEngine)((Item)this.jCEngine.getSelectedItem()).getElement():null);
		}
}
